package cn.yinyu.queue.module.ecg.dal.mysql.call;

import java.util.*;

import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.yinyu.queue.framework.mybatis.core.mapper.BaseMapperX;
import cn.yinyu.queue.module.ecg.dal.dataobject.call.CallDO;
import org.apache.ibatis.annotations.*;
import cn.yinyu.queue.module.ecg.controller.admin.call.vo.*;

/**
 * 叫号 Mapper
 *
 * @author 马剑波
 */
@Mapper
public interface CallMapper extends BaseMapperX<CallDO> {

    default PageResult<CallDO> selectPage(CallPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CallDO>()
                .eqIfPresent(CallDO::getPatId, reqVO.getPatId())
                .likeIfPresent(CallDO::getPatName, reqVO.getPatName())
                .eqIfPresent(CallDO::getPatGender, reqVO.getPatGender())
                .eqIfPresent(CallDO::getSeqNum, reqVO.getSeqNum())
                .eqIfPresent(CallDO::getPassed, reqVO.getPassed())
                .eqIfPresent(CallDO::getExpired, reqVO.getExpired())
                .eqIfPresent(CallDO::getCalled, reqVO.getCalled())
                .eqIfPresent(CallDO::getJumpFlag, reqVO.getJumpFlag())
                .eqIfPresent(CallDO::getRoomId, reqVO.getRoomId())
                .likeIfPresent(CallDO::getRoomName, reqVO.getRoomName())
                .eqIfPresent(CallDO::getBedNo, reqVO.getBedNo())
                .betweenIfPresent(CallDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CallDO::getId));
    }

    @Select("select * from yy_queue.call_patient where pat_id = #{patId} order by id desc limit 1")
    CallDO getLatestPatientCall(@Param("patId") String patId);

    // call_type 0  大屏
    @Select("select * from yy_queue.call_patient where called = 0 and call_type = 0 order by id limit 1")
    CallDO getNextCall( );

    // call_type 1  诊间屏
    @Select("select * from yy_queue.call_patient where called = 0 and call_type = 1 and room_id = #{roomId} order by id limit 1")
    CallDO getNextInstallCall(@Param("roomId") Long roomId);

    //@Update("TRUNCATE TABLE yy_queue.call_patient")  保持当天数据
    @Delete("delete from yy_queue.call_patient where TO_DAYS(book_date) != TO_DAYS(NOW())")
    void clearCall();
}
